package com.sg.hms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sg.hms.Model.Food;
import com.sg.hms.Model.F_PageBean;
import com.sg.hms.Model.Role;
import com.sg.hms.Service.FoodService;
import com.sg.hms.ServiceImpl.FoodServiceImpl;

/**
 * 
 * @author 张杨
 *
 */
public class FindFoodController extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		FoodService fService=new FoodServiceImpl();
		String p=req.getParameter("page");
		int page=0;
		if (p==null) {
			page=1;
		}else {
			page=Integer.parseInt(p);
		}
		F_PageBean pageBean=new F_PageBean(page, 10);
		HttpSession session=req.getSession();
		Role role= (Role) session.getAttribute("currentrole");
		
		session.setAttribute("page", this.getPage(fService.countFood(role.getRoleId()), page, pageBean.getPageSize()));
		List<Food> foods=fService.findFood(role.getRoleId(),pageBean);
		session.setAttribute("foods", foods);
		req.getRequestDispatcher("page/food.jsp").forward(req, resp);

		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
			doGet(req, resp);
	}
	/**
	 *  每次生成新的页码
	 * @param totalNum  传入数据的总数
	 * @param currentPage  传入当前的页码
	 * @param pageSize  传入 需要按照多少条来分
	 * @return
	 */
	private  String  getPage(int totalNum,int currentPage,int pageSize){
		int totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuffer page =  new StringBuffer();
		page.append("<li><a style='background-color:black' href='foodManage?page=1'>首页</a></li>");
		//首页
		if(currentPage==1){
			page.append("<li ><a style='background-color:black' href='#'>上一页</a></li>");
		}else{
			page.append("<li><a style='background-color:black' href='foodManage?page="+(currentPage-1)+"'>上一页</a></li>");
		}
		//生成页码     当前页 2        0                  4
		for (int i = currentPage-3; i < currentPage+2; i++) {
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					page.append("<li style='background-color:black' class='active'><a href='#'>"+i+"</a></li>");					 
				}else{
					page.append("<li><a style='background-color:black' href='foodManage?page="+i+"'>"+i+"</a></li>");
				}
		}
	//尾页
		if(currentPage==totalPage){
			page.append("<li><a style='background-color:black' href='#'>下一页</a></li >");
		}else{
			page.append("<li><a style='background-color:black' href='foodManage?page="+(currentPage+1)+"'>下一页</a></li>");
		}
	
		
		page.append("<li><a style='background-color:black' href='foodManage?page="+totalPage+"'>尾页</a></li>");
		return  page.toString();
	}
}
